# 引入线程池包
from concurrent.futures import ThreadPoolExecutor
import time

def crawl(url):
    msg = f'请求地址为：{url}'
    time.sleep(3)
    # print(msg)
    return msg;



if __name__ == '__main__':
    base_url = 'http://www.baidu.com?page={}'
    # 创建包含3条线程的线程池
    pool = ThreadPoolExecutor(max_workers=3);

    for i in range(5):
        # 向线程池提交一个任务
        future1 = pool.submit(crawl, base_url.format(i))
        # 判断future1的任务是否结束
        # print(future1.done())
        # 获取future1的结果
        print(future1.result())

    # # 向线程池提交一个任务
    # future1 = pool.submit(crawl, base_url.format(1))
    # # 判断future1的任务是否结束
    # # print(future1.done())
    # # 获取future1的结果
    # print(future1.result())

    # 关闭线程池 当不想提交任何任务时，调用 ThreadPoolExecutor 对象的 shutdown() 方法来关闭线程池。
    pool.shutdown()
